//
//  ZYMainController.m
//  Joynet
//
//  Created by admin on 2017/7/24.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ZYMainController.h"
#import "WYHomeController.h"
#import "WYChoiceController.h"
#import "WYCollectionController.h"
#import "WYProfileController.h"
#import "MCTabBar.h"
#import "WYAddController.h"
#import "WYBaseNavigationController.h"
@interface ZYMainController ()<UITabBarControllerDelegate>
@property (nonatomic, strong) MCTabBar *mcTabbar;
@end

@implementation ZYMainController

- (void)viewDidLoad {
    [super viewDidLoad];
    _mcTabbar = [[MCTabBar alloc] init];
    [_mcTabbar.centerBtn addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    //选中时的颜色
    _mcTabbar.tintColor = [UIColor colorWithRed:27.0/255.0 green:118.0/255.0 blue:208/255.0 alpha:1];
    //透明设置为NO，显示白色，view的高度到tabbar顶部截止，YES的话到底部
    _mcTabbar.translucent = NO;
    //利用KVC 将自己的tabbar赋给系统tabBar
    [self setValue:_mcTabbar forKeyPath:@"tabBar"];
     self.delegate = self;
     [self setupAllChildViewControllers];
    
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}





- (void)setupAllChildViewControllers {
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor grayColor], NSForegroundColorAttributeName,
                                                       nil] forState:UIControlStateNormal];
    
    UIColor *titleHighlightedColor = [UIColor blackColor];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:titleHighlightedColor, NSForegroundColorAttributeName,nil] forState:UIControlStateSelected];
    
    
    WYHomeController *vc=[[WYHomeController alloc]init];
    [self setupChildViewController:vc title:@"首页" imageName:@"icon_tabbar_home_normal" selectedImageName:@"icon_tabbar_home_select"];
    
    WYChoiceController *vc1=[[WYChoiceController alloc]init];
    [self setupChildViewController:vc1 title:@"选包" imageName:@"icon_tabbar_choice_normal" selectedImageName:@"icon_tabbar_choice_select"];
    
    WYAddController *vc5=[[WYAddController alloc]init];
    [self setupChildViewController:vc5 title:@"共享" imageName:@"" selectedImageName:@""];
    
    WYCollectionController *vc2=[[WYCollectionController alloc]init];
    [self setupChildViewController:vc2 title:@"收藏" imageName:@"icon_tabbar_collection_normal" selectedImageName:@"icon_tabbar_collection_select"];
    WYProfileController *vc3=[[WYProfileController alloc]init];
    [self setupChildViewController:vc3 title:@"我的" imageName:@"icon_tabbar_me_normal" selectedImageName:@"icon_tabbar_me_select"];
    
    
    
   
}
- (void)setupChildViewController:(UIViewController *)childVc title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
   // childVc.tabBarItem.title=title;
    childVc.tabBarItem.image = [UIImage imageNamed:imageName];
    UIImage *selectedIamge = [UIImage imageNamed:selectedImageName];
    childVc.tabBarItem.selectedImage = [selectedIamge imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    childVc.title = title;
   // CGFloat offset = 5.0;
    
    //tabBar图片居中显示，不显示文字
    
   // childVc.tabBarItem.imageInsets = UIEdgeInsetsMake(offset, 0, -offset, 0);
    WYBaseNavigationController *navVc = [[WYBaseNavigationController alloc] initWithRootViewController:childVc];
    [self addChildViewController:navVc];
}


- (void)buttonAction:(UIButton *)button{
    NSLog(@"%@",@"点击了中间按钮");
    
    //self.selectedIndex = 2;//关联中间按钮
   // [self rotationAnimation];
}

//tabbar选择时的代理
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 2){//选中中间的按钮
       // [self rotationAnimation];
    }else {
        [_mcTabbar.centerBtn.layer removeAllAnimations];
    }
}
//旋转动画
- (void)rotationAnimation{
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI*2.0];
    rotationAnimation.duration = 3.0;
    rotationAnimation.repeatCount = HUGE;
    [_mcTabbar.centerBtn.layer addAnimation:rotationAnimation forKey:@"key"];
}








@end
